<form class="layui-form seller-form"  action="" >
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">部/院：</label>
            <div class="layui-input-inline" id="dep_box">
                <input type="text" name="dep_id" id="dep_id" class="layui-input" readonly="readonly" value="{$dep}"></input>
                <input type="hidden" name="applyid" value="{$applyid}" />
                <!-- <select name="dep_id" lay-filter="dep_id" id="dep_id" lay-search>
                    <option value=""></option>
                    <option value="1">1</option>
                </select> -->
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">专业：</label>
            <div class="layui-input-inline" id="specialty_box">
                <input type="text" name="specialty_id" id="specialty_id" class="layui-input" readonly="readonly" value="{$specialty}"></input>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">班级：</label>
            <div class="layui-input-inline" id="class_box">
                <input type="text" name="class_id" id="class_id" class="layui-input" readonly="readonly" value="{$class}"></input>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">方向：</label>
            <div class="layui-input-inline" id="dir_box">
                <input type="text" id="dir" name="dir" class="layui-input" readonly="readonly" value="{$dir}"></input>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">学号：</label>
            <div class="layui-input-inline" id="xh_box">
                <input type="text" id="xh" name="xh" class="layui-input" value="" placeholder="请输入学号"></input>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">姓名：</label>
            <div class="layui-input-inline">
                <input id="xm" type="text" name="xm" lay-verify="xm" placeholder="请输入姓名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">申请人：</label>
            <div class="layui-input-inline" id="teacher_box">
                <input type="text" id="teacherid" name="teacherid" class="layui-input" readonly="readonly" value="{$teacher}"></input>
            </div>
        </div>

        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="search"><i class="iconfont icon-chaxun"></i>查询</button>
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="export"><i class="iconfont icon-clouddownload"></i>导出</button> 
        </div>
    </div>
</form>

<div id="exportDetail" style="display: none;">
    <form class="layui-form export-form" action="">
        <div class="layui-form-item">
            <div class="layui-margin-10">
                <blockquote class="layui-elem-quote layui-text">
                    请先筛选要导出的明细，默认导出全部
                </blockquote>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">任务名称：</label>
                <input type="text" name="taskname" lay-verify="title" style="width:200px;" placeholder="请输入任务名称"
                    autocomplete="off" class="layui-input">
            </div>
        </div>
    </form>
</div>

<div class="table-body">
    <table id="drawdetailTable" lay-filter="drawdetailTable"></table>
</div>

<script>
    layui.use(['table','form','layer','laydate'],function(){
        var layer = layui.layer, table = layui.table,form = layui.form,date = layui.laydate;
        // 定义变量保存当前页面和当前页面数据条数
        var currPage; 
        var dataNum;

        //执行渲染
        table.render({
            elem: '#drawdetailTable', //指定原始表格元素选择器（推荐id选择器）
            height: 'full-165',
            cellMinWidth: '80',
            page: 'true',
            limit:'20',
            id:'drawdetailTable',
            url: "{:url('BookDraw/bookdrawdetail')}" + '?applyid={$applyid}',
            cols: [[ //标题栏
            { type: 'numbers' },
                { field: 'id', title: 'ID', hide:true},
                { field: 'applyid', hide:true, title: 'ApplyID'},
                { field: 'xh', sort: true, title: '学号', width: 120, align: 'center' },
                { field: 'xm', sort: true, title: '姓名', width: 80, align: 'center' },
                { field: 'dep', sort: true, title: '部/院', width: 200, align: 'center' },
                { field: 'specialty', sort: true, title: '专业', width: 200, align: 'center' },
                { field: 'class', sort: true, title: '班级', width: 200, align: 'center' },
                { field: 'dir', sort: true, title: '方向', width: 80, align: 'center' },
                { field: 'mobile', title: '申请人手机号', width: 120, align: 'center' },
                { field: 'teacher', title: '申请人', width: 80, align: 'center' },
                { field: 'is_qs', sort: true, title: '状态', width: 80, align: 'center' },
                {
                    field: 'option', title: '操作', templet: function (d) {
                        var html = '<a class="layui-btn layui-btn-xs del layui-btn-danger" name="del" data-id="' + d.id + '">删除</a>'; 
                        return html;
                    }, align: 'center', width: 80
                }
            ]], //设置表头
            done: function(res, curr, count){
                // 给变量赋值 
                dataNum = res.data.length;
                currPage = curr;
            }
        });

        //删除申请
        $(document).on('click', '.del', function () { 
            var id = $(this).attr('data-id');
            $("a[name='del']").addClass("layui-btn-disabled");
            $("a[name='del']").attr("disabled", true);
            layer.confirm('您确定删除该学生的申请记录吗？', {
                btn: ['确认', '取消'], //按钮
                cancel:function(index, layero){
                    $("a[name='del']").removeClass("layui-btn-disabled");
                    $("a[name='del']").removeAttr("disabled");
                }
            }, function () {
                JsPost("{:url('BookDraw/bookdrawdelstudent')}", {id:id}, function (res) {
                    $("a[name='del']").removeClass("layui-btn-disabled");
                    $("a[name='del']").removeAttr("disabled");
                    
                    //删除逻辑中判断只有一条数据时应该刷新前一页信息
                    layer.msg(res.msg);
                    if(res.status){
                        if(dataNum == 1){
                            currPage = currPage - 1;
                        }

                        table.reload('drawdetailTable', {
                            page: {
                                curr: currPage
                            }
                        });
                        return false;
                    }
                });
            }, function () {
                $("a[name='del']").removeClass("layui-btn-disabled");
                $("a[name='del']").removeAttr("disabled");
                layer.close(1);
            });
        }); 

        form.on('submit(search)', function(data){
            layui.table.reload('drawdetailTable', {
                where: {"applyid":data.field.applyid, "xh":data.field.xh, "xm":data.field.xm}
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

      
        //导出
        form.on('submit(export)', function (data) {
            var rownum = layui.table.cache["drawdetailTable"].length;
            if(!rownum){
                layer.msg('没有需要导出的数据！', {time: 1300});
                return false;
            }

            layer.open({
                id: 'LAY_bookbatchdraw_detail',  // 设定id，防止重复弹出
                type: 1,
                title: '明细导出',
                area: ['400px', '240px'], //宽高
                btn: ['确定', '取消']
                ,shadeClose: false
                ,content: $("#exportDetail").html()
                ,success: function () {
                    $(':focus').blur();
                    this.enterEsc = function(event){
                        if(event.keyCode === 13){
                            if(!$(".layui-layer-btn0").hasClass("layui-btn-disabled")){
                                $(".layui-layer-btn0").addClass("layui-btn-disabled");

                                //判断是否有选中
                                var filter = $(".seller-form").serialize();
                                $(".export-form:last").append("<input type='hidden' name='filter' value='" + filter + "'>");
                                var data = $(".export-form:last").serializeArray();
                                data.push({ 'name': 'model', 'value': 'BookApplyDetail' });

                                if($.trim(data[0]['value']) == '') {
                                    layer.msg('请填写任务名称！', { time: 1300 });
                                    $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                    return false;
                                }

                                JsPost("{:url('Ietask/export')}", data, function (res) {
                                        layer.msg(res.msg, { time: 1500 }, function () {
                                            if (res.status) {
                                                layer.closeAll();
                                                //执行跳转
                                                var topLayui = parent === self ? layui : top.layui;
                                                topLayui.index.openTabsPage('/manage/ietask/index.html', '导入导出');
                                            } else {
                                                $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                            }
                                        });
                                    }
                                );
                            }
                            return false; //阻止系统默认回车事件
                        }
                    };
                    $(document).on('keydown', this.enterEsc);	//监听键盘事件，关闭层
                },
                yes: function () {
                    if(!$(".layui-layer-btn0").hasClass("layui-btn-disabled")){
                        $(".layui-layer-btn0").addClass("layui-btn-disabled");

                        //判断是否有选中
                        var filter = $(".seller-form").serialize();
                        $(".export-form:last").append("<input type='hidden' name='filter' value='" + filter + "'>");
                        var data = $(".export-form:last").serializeArray();
                        
                        if($.trim(data[0]['value']) == '') {
                            layer.msg('请填写任务名称！', { time: 1300 });
                            $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                            return false;
                        }

                        data.push({ 'name': 'model', 'value': 'BookApplyDetail' });
                        JsPost("{:url('Ietask/export')}", data, function (res) {
                                layer.msg(res.msg, { time: 1500 }, function () {
                                    if (res.status) {
                                        layer.closeAll();
                                        //执行跳转
                                        var topLayui = parent === self ? layui : top.layui;
                                        topLayui.index.openTabsPage('/manage/ietask/index.html', '导入导出');
                                    } else {
                                        $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                    }
                                });
                            }
                        );
                    }
                }, btn2: function () {
                    layer.closeAll();
                }, end: function(){
                    $(document).off('keydown', this.enterEsc);	        //解除键盘关闭事件
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    })
</script>
